package com.mf.ergate.service.opt.impl;

import com.mf.ergate.api.auth.model.dto.RoleUserRelationQueryDto;
import com.mf.ergate.dao.opt.EmployeeInfoDao;
import com.mf.ergate.domain.model.auth.RoleUserRelationEntity;
import com.mf.ergate.domain.model.opt.EmployeeInfoEntity;
import com.mf.ergate.domain.model.sys.FileStorageInfoEntity;
import com.mf.ergate.service.GenericService;
import com.mf.ergate.service.auth.RoleUserRelationService;
import com.mf.ergate.service.opt.EmployeeInfoService;
import com.mf.ergate.service.sys.FileStorageInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工信息Service层实现类
 * User: zhaoming
 * DateTime: 2019-04-16
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class EmployeeInfoServiceImpl extends GenericService<EmployeeInfoEntity> implements EmployeeInfoService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EmployeeInfoDao employeeInfoDao;
    @Autowired
    private RoleUserRelationService roleUserRelationService;
    @Autowired
    private FileStorageInfoService fileStorageInfoService;

    /**
     * 删除员工及相关信息(逻辑删除)
     * @param employeeInfoEntity 员工信息Entity
     * @return 执行结果
     */
    public Boolean deleteCompleteByUniqueNo(EmployeeInfoEntity employeeInfoEntity){
        //删除头像
        EmployeeInfoEntity temp = employeeInfoDao.selectByUniqueNo(employeeInfoEntity.getUniqueNo());
        if(StringUtils.isNotBlank(temp.getEmployeeAvatar())){
            FileStorageInfoEntity fileStorageInfoEntity = new FileStorageInfoEntity();
            fileStorageInfoEntity.setUniqueNo(temp.getEmployeeAvatar());
            fileStorageInfoEntity.setUpdateUserNo(employeeInfoEntity.getUpdateUserNo());
            fileStorageInfoEntity.setUpdateUserName(employeeInfoEntity.getUpdateUserName());
            fileStorageInfoEntity.setUpdateTime(employeeInfoEntity.getUpdateTime());
            fileStorageInfoService.delete(fileStorageInfoEntity);
        }
        //删除用户与角色关联信息
        RoleUserRelationQueryDto roleUserRelationQueryDto = new RoleUserRelationQueryDto();
        roleUserRelationQueryDto.setUserNo(employeeInfoEntity.getUniqueNo());
        List<RoleUserRelationEntity> roleUserRelationEntities = roleUserRelationService.selectByCondition(roleUserRelationQueryDto);
        if (CollectionUtils.isNotEmpty(roleUserRelationEntities)) {
            for (RoleUserRelationEntity data : roleUserRelationEntities) {
                data.setUpdateTime(employeeInfoEntity.getUpdateTime());
                data.setUpdateUserNo(employeeInfoEntity.getUpdateUserNo());
                data.setUpdateUserName(employeeInfoEntity.getUpdateUserName());
            }
            roleUserRelationService.deleteByUniqueNos(roleUserRelationEntities);
        }
        return employeeInfoDao.deleteByUniqueNo(employeeInfoEntity);
    }
}
